package com.example.home.bus.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.home.R;
import com.example.home.bus.adapter.BusRouteAdapter;
import com.example.home.bus.bean.BusDetailBean;
import com.example.home.bus.bean.BusListBean;
import com.example.home.tool.OKHttpTool;
import com.google.gson.Gson;

public class BusDetail extends AppCompatActivity {

    private BusListBean.RowsBean rowsBean;
    private ImageView back;
    private TextView name;
    private TextView first;
    private TextView end;
    private TextView startTime;
    private TextView endTime;
    private TextView mileage;
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bus_detail);
        Intent intent = getIntent();
        String busId = intent.getStringExtra("busId");
        Gson gson = new Gson();
        rowsBean = gson.fromJson(busId, BusListBean.RowsBean.class);

        initView();

        getBusPath();
    }

    private void getBusPath() {
        OKHttpTool.doNetRequest("prod-api/api/bus/stop/list?linesId="+rowsBean.getId(),
                OKHttpTool.GET,
                BusDetailBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        BusDetailBean busDetailBean = (BusDetailBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindView(busDetailBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void bindView(BusDetailBean busDetailBean) {
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        BusRouteAdapter busRouteAdapter = new BusRouteAdapter(busDetailBean.getRows(),BusDetail.this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(BusDetail.this, LinearLayoutManager.HORIZONTAL, false);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(busRouteAdapter);

    }

    private void initView() {

        back = findViewById(R.id.back);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BusDetail.this.finish();
            }
        });

        name = findViewById(R.id.name);
        name.setText(rowsBean.getName());

        first = findViewById(R.id.first);
        first.setText("起:"+rowsBean.getFirst());

        end = findViewById(R.id.end);
        end.setText("终:"+rowsBean.getEnd());

        startTime = findViewById(R.id.startTime);
        startTime.setText("首:"+rowsBean.getStartTime());

        endTime = findViewById(R.id.endTime);
        endTime.setText("末:"+rowsBean.getEndTime());

        mileage = findViewById(R.id.mileage);
        mileage.setText(String.valueOf(rowsBean.getPrice())+" 元");

        viewPager = findViewById(R.id.viewPager);

    }
}